#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "to.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    database = cv::imread(ImagePath.toStdString() + "20230207202358.jpg"); //自己修改路径即可
    database.features = stools.getFaceFeatures(database.faceImage);
    ConnectSlots();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::OpenCamera()
{
    //ui->CameraView->width()
    cap.set(cv::CAP_PROP_FRAME_WIDTH, 800);
    //ui->CameraView->height()
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, 600);
    cap.open(0);
//    qDebug() << cap.get(cv::CAP_PROP_FRAME_WIDTH) << ' '
//             << cap.get(cv::CAP_PROP_FRAME_HEIGHT) << "a\n";
    timer->start(50); // 单位ms,可以自己调整
}

void MainWindow::closeCamera()
{
    timer->stop();
    cap.release();
    ui->CameraView->clear();
}

void MainWindow::GetFrame()
{
    cap.read(cv_temp);
 //   qDebug() << cv_temp.cols << ' ' << cv_temp.rows << '\n';
    _data = cv_temp;
    _data.faceRect = stools.getBiggestFaceRect(_data.faceImage);
    _data.features = stools.getFaceFeatures(_data.faceImage);
    cv::Rect rect;
    rect.x = _data.faceRect.x;
    rect.y = _data.faceRect.y;
    rect.height = _data.faceRect.height;
    rect.width = _data.faceRect.width;
    cv::rectangle(cv_temp, rect, CV_RGB(0, 0, 255), 4, 8, 0);
    QImage img = YQ::toQImage(cv_temp);
    QImage _img = img.scaled(ui->CameraView->width(),ui->CameraView->height());
    ui->CameraView->setPixmap(QPixmap::fromImage(_img));
    qDebug() << stools.compareFace(_data.features.get(), database.features.get()) << '\n';
}



void MainWindow::ConnectSlots()
{
    //QTimer
    connect(timer,SIGNAL(timeout()),this,SLOT(GetFrame()));

    //OpenCamera
    connect(ui->pushButton_open, SIGNAL(clicked()), this, SLOT(OpenCamera()));
    //close
    connect(ui->pushButton_close, SIGNAL(clicked()), this, SLOT(closeCamera()));

}
